package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppCompatActivity;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.GestureLockView;
import com.hundsun.yr.universal.p2p.HsApplication;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.core.FingerprintCore;

/**
 * 手势密码
 *
 * @author tonglj
 */
public class GestureLockLoginActivity extends BaseAppCompatActivity implements OnClickListener {
    private GestureLockView gesture_view;
    //	private View tv_skip,img_close;
//    private TextView tvForgetGesture;
    private TextView tvChangeAccount;
    private LinearLayout ll_finger_login;

    private HSSharedPreferences mConfigSP;
    private HSSharedPreferences sharedPreferences;
    private TextView gesture_login_title;
    private FingerprintCore mFingerprintCore;

    private int FINGER_LOGIN = 1;

    @Override
    public void onClick(View v) {
        if (isFastDoubleClick()){
            return;
        }

        switch (v.getId()) {
//            case R.id.tv_forget_gesture:
//                Bundle bundle = new Bundle();
//                bundle.putString("comefrom", "GestureLockLoginActivity");
//                readyGo(PasswordVerifyDialogActivity.class, bundle);
//                break;
            case R.id.tv_change_account:
                sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
                sharedPreferences.clear(mContext);

                BaseAppManager.getInstance().clearToTop();
                HsApplication.goGesture = true;
                HsApplication.isSrcreenOn = false;
                Bundle bundle2 = new Bundle();
                bundle2.putString(Extras.NAVIGATE_TAG, "HOME");
                readyGoThenKill(MainActivity.class, bundle2);
                break;
            default:
                break;
        }
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_gesturelogin;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected void initViewsAndEvents() {
//        setSystemBarTintDrawable(getResources().getDrawable(R.color.white));
        findViews();
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        mConfigSP = new HSSharedPreferences(mContext, SPPath.file_configInfo);
        gesture_view.setKeySet(mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE));
        mFingerprintCore = new FingerprintCore(mContext);

        // TODO: 2017/6/30 根据是否设置指纹锁显示或者隐藏
        if (mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_FINGERPRINT) && !mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE)){
            //设置了指纹，没设置手势
            readyGoForResult(FingerprintLockActivity.class,FINGER_LOGIN);
            ll_finger_login.setVisibility(View.VISIBLE);
            gesture_view.setVisibility(View.GONE);
//            tvForgetGesture.setVisibility(View.GONE);
        } else if (mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_FINGERPRINT) && mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE)){
            //都设置了
            readyGoForResult(FingerprintLockActivity.class,FINGER_LOGIN);
            ll_finger_login.setVisibility(View.VISIBLE);

            //没有设置过指纹锁的话才但是设置了手势锁
            gesture_view.setOnCompleteListener(new GestureLockView.OnCompleteListener() {
                @Override
                public void onError() {
                    sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
                    sharedPreferences.clear(mContext);
                    BaseAppManager.getInstance().clearToTop();
                    HsApplication.goGesture = true;
                    HsApplication.isSrcreenOn = false;
                    Bundle bundle2 = new Bundle();
                    bundle2.putString(Extras.NAVIGATE_TAG, "HOME");
                    readyGoThenKill(MainActivity.class, bundle2);
                }

                @Override
                public void onComplete(String result) {
                    if (TextUtils.equals(result, mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE))) {
                        finish();
                    }
                }
            });
//            tvForgetGesture.setOnClickListener(this);
        }else {
            //没有设置过指纹锁的话才但是设置了手势锁
            gesture_view.setOnCompleteListener(new GestureLockView.OnCompleteListener() {
                @Override
                public void onError() {
                    sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
                    sharedPreferences.clear(mContext);
                    BaseAppManager.getInstance().clearToTop();
                    HsApplication.goGesture = true;
                    HsApplication.isSrcreenOn = false;
                    Bundle bundle2 = new Bundle();
                    bundle2.putString(Extras.NAVIGATE_TAG, "HOME");
                    readyGoThenKill(MainActivity.class, bundle2);
                }

                @Override
                public void onComplete(String result) {
                    if (TextUtils.equals(result, mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE))) {
                        finish();
                    }
                }
            });
//            tvForgetGesture.setOnClickListener(this);

            ll_finger_login.setVisibility(View.GONE);
        }

        tvChangeAccount.setOnClickListener(this);
        ll_finger_login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                readyGoForResult(FingerprintLockActivity.class,FINGER_LOGIN);
            }
        });
    }

    private void findViews() {
        gesture_view = findView(R.id.login_gesture_view);
//        tvForgetGesture = findView(R.id.tv_forget_gesture);
        tvChangeAccount = findView(R.id.tv_change_account);
        ll_finger_login = findView(R.id.ll_finger_login);
        gesture_login_title = findView(R.id.gesture_login_title);
    }

    @Override
    protected void onResume() {
        HsApplication.goGesture = false;
        HsApplication.isSrcreenOn = false;
        super.onResume();
    }


    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


//    @Override
//    protected boolean isApplyKitKatTranslucency() {
//        // TODO Auto-generated method stub
//        return false;
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

//	@Override
//	protected void onDestroy() {
//		HsApplication.goGeture = false;
//		super.onDestroy();
//	}

    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 2){
            finish();
        }
    }
}
